package com.yx.frame.builder;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import com.yx.frame.builder.xml.MethodParmXml;

import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class MethodParms extends Composite {
	Shell window = null;
	private Table parmTable;
	private String methodType;
	private TabItem SqlsTab;
	private TabItem MenCacheTab;
	private MethodParms methodParms;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public MethodParms(Composite parent, int style, Shell win, TabItem sTab,
			TabItem mTab) {
		super(parent, style);
		this.window = win;
		this.SqlsTab = sTab;
		this.MenCacheTab = mTab;
		methodParms = this;

		Button button = new Button(this, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				MethodParmValidatorSelect methodParmValidatorSelect = new MethodParmValidatorSelect(
						window, SWT.NONE, parmTable, -1, window, methodType,
						methodParms);
				methodParmValidatorSelect.open();
			}
		});
		button.setBounds(439, 10, 45, 27);
		button.setText("添加");

		Button button_2 = new Button(this, SWT.NONE);
		button_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int idx = parmTable.getSelectionIndex();
				if (idx >= 0) {
					parmTable.remove(idx);
					List<String> listTypeParms = new ArrayList<String>();
					List<String> parmKeys = new ArrayList<String>();
					for (int i = 0; i < parmTable.getItemCount(); i++) {
						parmTable.getItem(i).setText(0, "" + (i + 1));
						if ("List".equals(parmTable.getItem(i).getText(2))) {
							listTypeParms.add(parmTable.getItem(i).getText(1));
						}
						parmKeys.add(parmTable.getItem(i).getText(1));
					}
					if ("update".equals(methodType)) {
						((UpdateContent) SqlsTab.getControl())
								.setLoopbyListParms(listTypeParms);
					} else {
						((MencacheContent) MenCacheTab.getControl())
								.refreshParms(parmKeys);
					}

				}
			}
		});
		button_2.setText("删除");
		button_2.setBounds(589, 10, 45, 27);

		parmTable = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		parmTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				int sIdx = parmTable.getSelectionIndex();
				if (sIdx >= 0) {
					MethodParmValidatorSelect methodParmValidatorSelect = new MethodParmValidatorSelect(
							window, SWT.NONE, parmTable, sIdx, window,
							methodType, methodParms);
					methodParmValidatorSelect.open();
				}
			}
		});
		parmTable.setBounds(10, 39, 624, 338);
		parmTable.setHeaderVisible(true);
		parmTable.setLinesVisible(true);
		TableColumn tc1 = new TableColumn(parmTable, SWT.CENTER);
		TableColumn tc2 = new TableColumn(parmTable, SWT.LEFT);
		TableColumn tc3 = new TableColumn(parmTable, SWT.LEFT);
		TableColumn tc4 = new TableColumn(parmTable, SWT.LEFT);
		TableColumn tc5 = new TableColumn(parmTable, SWT.LEFT);
		// TableColumn tc6 = new TableColumn(parmTable, SWT.LEFT);
		TableColumn tc7 = new TableColumn(parmTable, SWT.LEFT);
		TableColumn tc8 = new TableColumn(parmTable, SWT.LEFT);
		tc1.setText("序号");
		tc2.setText("参数名称");
		tc3.setText("类型");
		tc4.setText("数组");
		tc5.setText("Sql序号");
		// tc6.setText("取值");
		tc7.setText("校验");
		tc8.setText("描述");
		tc1.setWidth(40);
		tc2.setWidth(120);
		tc3.setWidth(50);
		tc4.setWidth(50);
		tc5.setWidth(80);
		// tc6.setWidth(80);
		tc7.setWidth(140);
		tc8.setWidth(120);
		
		Button button_1 = new Button(this, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int selectIdx=parmTable.getSelectionIndex();
				if(parmTable.getItemCount()<1)return;
				if(selectIdx<0||selectIdx>=parmTable.getItemCount()-1){
					return ;
				}
				TableItem selectItem=parmTable.getItem(selectIdx);
				TableItem nextItem=parmTable.getItem(selectIdx+1);
				String tmp;
				for(int i=0;i<7;i++){
					tmp=selectItem.getText(i);
					selectItem.setText(i, nextItem.getText(i));
					nextItem.setText(i, tmp);
				}
				List<String> listTypeParms = new ArrayList<String>();
				List<String> parmKeys = new ArrayList<String>();
				for (int i = 0; i < parmTable.getItemCount(); i++) {
					parmTable.getItem(i).setText(0, "" + (i + 1));
					if ("true".equals(parmTable.getItem(i).getText(3))) {
						listTypeParms.add(parmTable.getItem(i).getText(1));
					}
					parmKeys.add(parmTable.getItem(i).getText(1));
				}
				if ("update".equals(methodType)) {
					((UpdateContent) SqlsTab.getControl())
							.setLoopbyListParms(listTypeParms);
				} else {
					((MencacheContent) MenCacheTab.getControl())
							.refreshParms(parmKeys);
				}
				parmTable.setSelection(selectIdx+1);
						
			}
		});
		button_1.setText("下移");
		button_1.setBounds(489, 10, 45, 27);
		
		Button button_3 = new Button(this, SWT.NONE);
		button_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int selectIdx=parmTable.getSelectionIndex();
				if(parmTable.getItemCount()<1)return;
				if(selectIdx<1||selectIdx>=parmTable.getItemCount()){
					return ;
				}
				TableItem selectItem=parmTable.getItem(selectIdx);
				TableItem upItem=parmTable.getItem(selectIdx-1);
				String tmp;
				for(int i=0;i<7;i++){
					tmp=selectItem.getText(i);
					selectItem.setText(i, upItem.getText(i));
					upItem.setText(i, tmp);
				}
				List<String> listTypeParms = new ArrayList<String>();
				List<String> parmKeys = new ArrayList<String>();
				for (int i = 0; i < parmTable.getItemCount(); i++) {
					parmTable.getItem(i).setText(0, "" + (i + 1));
					if ("true".equals(parmTable.getItem(i).getText(3))) {
						listTypeParms.add(parmTable.getItem(i).getText(1));
					}
					parmKeys.add(parmTable.getItem(i).getText(1));
				}
				if ("update".equals(methodType)) {
					((UpdateContent) SqlsTab.getControl())
							.setLoopbyListParms(listTypeParms);
				} else {
					((MencacheContent) MenCacheTab.getControl())
							.refreshParms(parmKeys);
				}
				parmTable.setSelection(selectIdx-1);
			}
		});
		button_3.setText("上移");
		button_3.setBounds(539, 10, 45, 27);

	}

	public void addParm(int id, String name, String type, String isMutiList,
			String sqlIdx, String validator, String comment) {
		TableItem item1 = new TableItem(parmTable, SWT.NONE);
		item1.setText(new String[] { "" + parmTable.getItemCount(), name, type,
				isMutiList, sqlIdx, validator, comment });
		List<String> listTypeParms = new ArrayList<String>();
		List<String> parmKeys = new ArrayList<String>();
		for (int i = 0; i < parmTable.getItemCount(); i++) {
			parmTable.getItem(i).setText(0, "" + (i + 1));
			if ("true".equals(parmTable.getItem(i).getText(3))) {
				listTypeParms.add(parmTable.getItem(i).getText(1));
			}
			parmKeys.add(parmTable.getItem(i).getText(1));
		}
		if ("update".equals(this.methodType)) {
			((UpdateContent) SqlsTab.getControl())
					.setLoopbyListParms(listTypeParms);
		} else {
			((MencacheContent) this.MenCacheTab.getControl())
					.refreshParms(parmKeys);
		}
	}

	public void editParm(int selectRow, String name, String type,
			String isMutiList, String sqlIdx, String validator, String comment) {
		TableItem item1 = parmTable.getItem(selectRow);
		item1.setText(1, name);
		item1.setText(2, type);
		item1.setText(3, isMutiList);
		item1.setText(4, sqlIdx);
		item1.setText(5, validator);
		item1.setText(6, comment);
		List<String> listTypeParms = new ArrayList<String>();
		List<String> parmKeys = new ArrayList<String>();
		for (int i = 0; i < parmTable.getItemCount(); i++) {
			parmTable.getItem(i).setText(0, "" + (i + 1));
			if ("true".equals(parmTable.getItem(i).getText(3))) {
				listTypeParms.add(parmTable.getItem(i).getText(1));
			}
			parmKeys.add(parmTable.getItem(i).getText(1));
		}
		if ("update".equals(this.methodType)) {
			((UpdateContent) SqlsTab.getControl())
					.setLoopbyListParms(listTypeParms);
		} else {
			((MencacheContent) this.MenCacheTab.getControl())
					.refreshParms(parmKeys);
		}
	}

	public void setType(String type) {
		methodType = type;		
	}

	public List<MethodParmXml> getParms() {
		List<MethodParmXml> methodParms = new ArrayList<MethodParmXml>();
		for (int i = 0; i < parmTable.getItemCount(); i++) {
			TableItem item = parmTable.getItem(i);
			MethodParmXml parm = new MethodParmXml(Integer.parseInt(item
					.getText(0)), item.getText(1), item.getText(2),
					item.getText(3), item.getText(4), item.getText(5),
					item.getText(6));
			methodParms.add(parm);
		}
		return methodParms;
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
